/*
 * Copyright 2016 drakeet. https://github.com/drakeet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.epfresh.adapter;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.activity.DetailGoodsActivity;
import com.epfresh.activity.HomeSecondAreaActivity;
import com.epfresh.bean.CountDownTime;
import com.epfresh.bean.HomeTop;
import com.epfresh.views.HomeCountDownView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import multitype.ItemViewBinder;
import multitype.OnRecyclerViewItemClickListener;


public class ViewBinderHomeSecond
    extends ItemViewBinder<HomeTop.SeckillBeanX.SeckillBean, ViewBinderHomeSecond.ViewHolder> implements View.OnClickListener{
    HomeCountDownView.OnCountDownListener countDownListener;

    public HomeCountDownView.OnCountDownListener getCountDownListener() {
        return countDownListener;
    }

    public void setCountDownListener(HomeCountDownView.OnCountDownListener countDownListener) {
        this.countDownListener = countDownListener;
    }

    @Override
    public void onClick(View v) {
        Intent more_activity = new Intent(v.getContext(),
                HomeSecondAreaActivity.class);
        v.getContext().startActivity(more_activity);
    }
    @NonNull @Override
    protected ViewHolder onCreateViewHolder(
        @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.view_home_second, parent, false);
        return new ViewHolder(view);
    }

    CountDownTime cdt;

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull HomeTop.SeckillBeanX.SeckillBean postList) {
        if(postList.getSeckillList() == null || postList.getSeckillList().size() == 0)
            return;
        holder.setPosts(postList.getSeckillList());
        cdt = new CountDownTime();
        cdt.setStartSubTime(postList.getBeginTime());//fineRecommendation.getString("beginTime"));
        cdt.setEndSubTime(postList.getEndTime());//fineRecommendation.getString("endTime"));
        cdt.setSysDate(postList.getSysDate());//fineRecommendation.getString("sysDate"));
        cdt.init();

        holder.cdv.setOnCountDownListener(countDownListener);
        long countDownTime = cdt.getCountDownTime();
        if (countDownTime == 0) {//活动已经结束
            holder.tv_second_hint.setVisibility(View.INVISIBLE);
        } else if (countDownTime > 0) {//活动进行中
            holder.cdv.showTimer(cdt, countDownTime, 1000);
            holder.tv_second_hint.setVisibility(View.INVISIBLE);
            //holder.tv_second_hint.setText("先下单先得哦");
        } else if (countDownTime < 0) {
            holder.cdv.showTimer(cdt, countDownTime, 1000);
            holder.tv_second_hint.setVisibility(View.VISIBLE);
            initSecKillHint(postList.getBeginTime(), postList.getSysDate(),holder.tv_second_hint);
        }
        holder.itemView.setOnClickListener(this);

    }

    static final class ViewHolder extends RecyclerView.ViewHolder {

        private RecyclerView recyclerView;
        private ViewBinderHomeSecondItemAdapter adapter;
        private TextView tv_second_hint;
        private HomeCountDownView cdv;

        private ViewHolder(@NonNull View itemView) {
            super(itemView);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.post_list);
            LinearLayoutManager layoutManager = new LinearLayoutManager(itemView.getContext());
            layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            recyclerView.setLayoutManager(layoutManager);
            adapter = new ViewBinderHomeSecondItemAdapter();
            recyclerView.setAdapter(adapter);

            tv_second_hint = (TextView) itemView.findViewById(R.id.tv_second_hint);
            cdv = (HomeCountDownView) itemView.findViewById(R.id.cdv);
        }


        private void setPosts(List<HomeTop.SeckillBeanX.SeckillBean.SeckillListBean> posts) {
            adapter.setPosts(posts);
            adapter.notifyDataSetChanged();
        }
    }

    void initSecKillHint(String beginTime, String nowTime,TextView tv_second_hint) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Calendar today = Calendar.getInstance();
            Calendar target = Calendar.getInstance();
            today.setTime(sdf.parse(nowTime));
            today.set(Calendar.HOUR_OF_DAY, 0);
            today.set(Calendar.MINUTE, 0);
            today.set(Calendar.SECOND, 0);

            target.setTime(sdf.parse(beginTime));
            target.set(Calendar.HOUR_OF_DAY, 0);
            target.set(Calendar.MINUTE, 0);
            target.set(Calendar.SECOND, 0);

            long intervalMilli = target.getTimeInMillis() - today.getTimeInMillis();
            int xcts = (int) (intervalMilli / (24 * 60 * 60 * 1000));
            if (xcts == 0) {
                tv_second_hint.setText("今天" + beginTime.substring(11, 16) + "开抢");
            } else if (xcts == 1) {
                tv_second_hint.setText("明天" + beginTime.substring(11, 16) + "开抢");
            } else {
                tv_second_hint.setText("后天" + beginTime.substring(11, 16) + "开抢");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
